package com.group.eams.controller;






import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.permission.Permission;
import com.group.eams.pojo.dto.RoleQueryDto;
import com.group.eams.pojo.dto.UpdateRolePermissionDto;
import com.group.eams.pojo.entity.Role;
import com.group.eams.pojo.entity.Role;
import com.group.eams.pojo.vo.RoleListVo;
import com.group.eams.service.RoleService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 角色管理(Role)表控制层
 *
 * @author 
 * @since 2024-10-30 12:58:29
 */
@RestController
@RequestMapping("/role")
public class RoleController  {
    /**
     * 服务对象
     */
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/updateRolePermission",method = RequestMethod.POST)
    @ApiOperation("修改角色的权限")
    public Result updateRolePermission(@RequestBody UpdateRolePermissionDto updateRolePermissionDto){
        roleService.updateRolePermission(updateRolePermissionDto);
        return Result.buildSuccess();
    }
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @Permission("role:list")//访问该方法必须具备该注解标记的权限才能访问
    @ApiOperation(value = "角色列表",httpMethod = "GET")
    public Result list(@RequestBody RoleQueryDto roleQueryDto){

        //1.准备分页条件
        Page<Role> page=new Page<>(roleQueryDto.getPage(),roleQueryDto.getLimit());

        //2.查询分页数据
        Page<RoleListVo> listVoPage = roleService.findRoleByWhere(page, roleQueryDto);

        //3.返回结果
        return Result.buildSuccess(listVoPage.getTotal(),listVoPage.getRecords());
    }

    @RequestMapping(value = "/find",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查找角色")
    public Result find(@RequestParam Long id){
        Role role = roleService.getById(id);
        return Result.buildSuccess(role);
    }
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation(value = "查找所有角色")
    public Result findAll(){
        List<Role> roleList = roleService.list();
        return Result.buildSuccess(roleList);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation("添加角色")
    public Result add(@RequestBody Role role){
        boolean result = roleService.save(role);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation("修改角色")
    public Result update(@RequestBody Role role){
        boolean result = roleService.updateById(role);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @ApiOperation("根据id删除角色")
    public Result delete(@RequestParam Long id){
        boolean result = roleService.removeById(id);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }


    @RequestMapping(value = "/batchRemove",method = RequestMethod.POST)
    @ApiOperation("批量删除角色")
    public Result batchRemove(@RequestBody BatchDeleteDto batchDeleteDto){
        boolean result = roleService.removeByIds(batchDeleteDto.getIds());
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }
}

